package com.novax.ex.activity.provider.controller;

import cn.hutool.core.util.StrUtil;
import com.novax.ex.activity.open.api.inner.MallStatementInnerApi;
import com.novax.ex.activity.open.model.query.MallStatementItemQuery;
import com.novax.ex.activity.open.model.query.MallStatementSalesQuery;
import com.novax.ex.activity.open.model.query.ParticipationRecordQuery;
import com.novax.ex.activity.open.model.query.PaymentRecordQuery;
import com.novax.ex.activity.open.model.response.MallStatementItemResponse;
import com.novax.ex.activity.open.model.response.MallStatementSalesResponse;
import com.novax.ex.activity.open.model.response.ParticipationRecordResponse;
import com.novax.ex.activity.open.model.response.PaymentRecordResponse;
import com.novax.ex.activity.provider.service.MallOrderWinnerService;
import com.novax.ex.activity.provider.service.MallPrizeOrderService;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.*;

/**
 * Description:参与费用配置
 *
 * @author Deucalion@novax.email
 * @date 2023/2/21 17:21
 */
@Slf4j
@RestController
public class MallStatementController extends BaseActivityController implements MallStatementInnerApi {
    @Resource
    private MallPrizeOrderService mallPrizeOrderService;
    @Resource
    private MallOrderWinnerService mallOrderWinnerService;

    @Override
    public ReturnResult<PageUtil<MallStatementSalesResponse>> sales(String language, MallStatementSalesQuery query) {
        if(StrUtil.isEmpty(query.getSort())){
            query.setSort("totalSalesVolume");
        }
        PageUtil<MallStatementSalesResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        pages.setCondition(condition);
        mallOrderWinnerService.salesPage(pages, MallStatementSalesResponse.class);
        return ReturnResult.success(pages);
    }

    @Override
    public ReturnResult<PageUtil<MallStatementItemResponse>> item(String language, MallStatementItemQuery query) {
        if(StrUtil.isEmpty(query.getSort())){
            query.setSort("createTime");
        }
        PageUtil<MallStatementItemResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        Set<String> mallProductIds = new HashSet<>();
        if (StrUtil.isEmpty(query.getMallProductId())) {
            condition.put("mallProductIds", null);
        } else {
            Collections.addAll(mallProductIds, query.getMallProductId().split(","));
            condition.put("mallProductIds", mallProductIds);
        }
        Set<String> userIds = new HashSet<>();
        if (StrUtil.isEmpty(query.getUserId())) {
            condition.put("userIds", null);
        } else {
            Collections.addAll(userIds, query.getUserId().split(","));
            condition.put("userIds", userIds);
        }
        pages.setCondition(condition);
        mallOrderWinnerService.itemPage(pages, MallStatementItemResponse.class);
        return ReturnResult.success(pages);
    }

    @Override
    public ReturnResult<PageUtil<ParticipationRecordResponse>> participationRecord(String language, ParticipationRecordQuery query) {
        if(StrUtil.isEmpty(query.getSort())){
            query.setSort("create_time");
        }
        PageUtil<ParticipationRecordResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        Set<String> mallProductIds = new HashSet<>();
        if (StrUtil.isEmpty(query.getMallProductId())) {
            condition.put("mallProductIds", null);
        } else {
            Collections.addAll(mallProductIds, query.getMallProductId().split(","));
            condition.put("mallProductIds", mallProductIds);
        }
        Set<String> userIds = new HashSet<>();
        if (StrUtil.isEmpty(query.getUserId())) {
            condition.put("userIds", null);
        } else {
            Collections.addAll(userIds, query.getUserId().split(","));
            condition.put("userIds", userIds);
        }
        Set<String> roundCounts = new HashSet<>();
        if (StrUtil.isEmpty(query.getRoundCount())) {
            condition.put("roundCounts", null);
        } else {
            Collections.addAll(roundCounts, query.getRoundCount().split(","));
            condition.put("roundCounts", roundCounts);
        }
        pages.setCondition(condition);
        mallPrizeOrderService.participationRecordPage(pages, ParticipationRecordResponse.class);
        return ReturnResult.success(pages);
    }

    @Override
    public ReturnResult<PageUtil<PaymentRecordResponse>> paymentRecord(String language, PaymentRecordQuery query) {
        if(StrUtil.isEmpty(query.getSort())){
            query.setSort("create_time");
        }
        PageUtil<PaymentRecordResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);

        pages.setCondition(condition);
        mallPrizeOrderService.paymentRecordPage(pages, PaymentRecordResponse.class);
        return ReturnResult.success(pages);
    }
}
